<?php
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | NewThink [ Think More,Think Better! ]
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | Copyright (c) 2016~2018 http://www.newthink.cc All rights reserved.
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | 版权所有：山西创泰网络科技有限公司
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | Author: lk Date:2019/5/11 Time:11:40
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

namespace app\common\service\basic;

class BasicConfig
{
    protected $pdo;
    public function __construct()
    {
        $dsn        = 'mysql:host=rm-bp11r52xk5623pn66o.mysql.rds.aliyuncs.com;dbname=huanxin';
        $userName   = 'huanxin';
        $passWord   = 'Sxctkjhuanxin!';
        $this->pdo        = new \PDO($dsn, $userName, $passWord);
        $this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
    }
    
    /**
     * 获取行业数据
     * @author Naomi
     * @param int $industry_type
     * @return array
     */
    public function getIndustryData(int $industry_type = 0)
    {
        $where = "";
        
        #类型
        !empty($industry_type) && $where .= "type = {$industry_type}";
        !empty($where) && $where .= " AND ";
        
        $where .= "delete_time = 0";
        $sql    = "SELECT id,name FROM `basic_industry` WHERE {$where}";
        $stmt   = $this->pdo->query($sql);
        $data   = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        
        return $data;
    }
    
    /**
     * 获取公司数据
     * @author Naomi
     * @param int $industry_id
     * @return array
     */
    public function getCompanyData(int $industry_id = 0)
    {
        $where = "";
        
        #行业
        !empty($industry_id) && $where .= "industry_id = {$industry_id}";
        !empty($where) && $where .= " AND ";
        
        $where .= "delete_time = 0";
        $sql    = "SELECT id,name FROM `basic_company` WHERE {$where}";
        $stmt   = $this->pdo->query($sql);
        $data   = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        
        return $data;
    }
    
    /**
     * 获取排口信息
     * @author Naomi
     * @param int $company_id
     * @return array
     */
    public function getDrainData(int $company_id = 0)
    {
        $where = "";
        
        #公司
        !empty($company_id) && $where .= "company_id = {$company_id}";
        !empty($where) && $where .= " AND ";
        
        $where .= "delete_time = 0";
        $sql    = "SELECT id,name FROM `basic_drain` WHERE {$where}";
        $stmt   = $this->pdo->query($sql);
        $data   = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        
        return $data;
    }
    
    /**
     * @author Naomi
     * @param array $param = ['type' => 0, 'company_id' => 0, 'machine_id' => 0, 'drain_id' => 0]
     * @return array
     */
    public function getFacilityData(array $param = [])
    {
        $where = "";
    
        #类型
        isset($param['type']) && $where .= "type = {$param['type']} AND ";
        #公司
        isset($param['company_id']) &&$where .= "company_id = {$param['company_id']} AND ";
        #机组
        isset($param['machine_id']) && $where .= "machine_id = {$param['machine_id']} AND ";
        #排口
        isset($param['drain_id']) && $where .= "drain_id = {$param['drain_id']} AND ";
    
        $where .= "delete_time = 0";
        $sql    = "SELECT id,number,name,name_cn,description FROM `basic_facility` WHERE {$where}";
        $stmt   = $this->pdo->query($sql);
        $data   = $stmt->fetchAll(\PDO::FETCH_ASSOC);
    
        return $data;
    }
}